package com.wyvoid.core.tag;

import com.wyvoid.core.ip.IPUtil;

import java.util.Collection;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang.StringUtils;

public class Functions {
    static SimpleDateFormat sf=new SimpleDateFormat();

    public static boolean contains(Object o1, Object o2) {
        if (o1 instanceof Collection) {
            return ((Collection) o1).contains(o2);
        }

        return false;
    }

    public static boolean isEmpty(Object o) {
        if (o instanceof Collection) {
            return ((Collection) o).isEmpty();
        } else if (o instanceof Map) {
            return ((Map) o).isEmpty();
        }

        return false;
    }

    public static String extractFormName(String className, String eventName) {
        String name = className.substring(className.lastIndexOf(".") + 1);
        if (name.endsWith("ActionBean")) {
            name = name.substring(0, name.length() - "ActionBean".length());
        } else if (name.endsWith("Action")) {
            name = name.substring(0, name.length() - "Action".length());
        }

        name = name.replaceAll("\\.", "_");
        name = name.toLowerCase();

        return eventName + "_" + name;
    }


    public static String ipAddr(String ip){
        return IPUtil.getAddress(ip);
    }

    public static String cnDate(Date date){
        if(date==null) return null;
        sf.applyPattern("yyyy-MM-dd");
        return sf.format(date);
    }

    public static String cnTime(Date date){
        sf.applyPattern("yyyy-MM-dd kk:mm");
        return sf.format(date);
    }

    public static String cnDate(){
        Date date=new Date();
        return cnDate(date);
    }

    public static String cnDate(String pattern){
        SimpleDateFormat tempSf=new SimpleDateFormat(pattern);
        return tempSf.format(new Date());
    }
    
    public static String cnTime(){
        Date date=new Date();
        return cnTime(date);
    }

    public static boolean isToday(String date){
        if(StringUtils.isEmpty(date)){
            return false;
        }
        sf.applyPattern("yyyy-MM-dd");
        try {
            return DateUtils.isSameDay(sf.parse(date),new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
